Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Xml
Imports System.ComponentModel
Imports System.Globalization
Imports C4F.DevKit.WebServices.Datasets

Namespace C4F.DevKit.WebServices
	''' <summary>
	''' Exposes 'get popular stories' functionality on Digg.com
	''' </summary>
	<ToolboxItem(True)> _
	Public Class Digg
		Inherits C4FWebServicesBase
		#Region "Private Members"

		'private string baseUrl = string.Format("{0}?appkey={1}",Resources.DiggBaseUrl,_appKey);
		''' <summary>
		''' private appKey propety holder
		''' </summary>
        Private _appKey As String = "http://c4fdevkit"
		#End Region

		#Region "Public Properties"
		''' <summary>
		''' Gets or sets the AppKey property required for the digg api
		''' </summary>
		''' <returns>The current AppKey</returns>
        <Category("Digg"), DefaultValue("http://c4fdevkit"), Description("Any  valid absolute URI in URL-Encoded Form.")> _
        Public Property AppKey() As String
            Get
                Return _appKey
            End Get
            Set(ByVal value As String)
                _appKey = value
            End Set
        End Property
		#End Region

		#Region "Private Properties"
		''' <summary>
		''' Request Url
		''' </summary>
		Private ReadOnly Property baseUrl() As String
			Get
                Return String.Format("{0}?appkey={1}", My.Resources.DiggBaseUrl, _appKey)
			End Get
		End Property
		#End Region

		#Region "Public Functions"
		''' <summary>
		''' Gets Popular story headlines
		''' </summary>
		''' <returns>List of stories returned</returns>
		Public Function GetHeadlines() As List(Of DiggStory)
			' Get the responses
			Dim responseData As String = MyBase.GetQueryResponse(baseUrl)

			' Parse the data and return results
			Return Digg.ParseResponseXml(responseData)
		End Function

		''' <summary>
		''' Gets Popular story headlines and returns the result in a dataset
		''' </summary>
		''' <returns>Dataset containing list of popular stories</returns>
		Public Function GetHeadlines_DS() As DS_Digg
			Dim stories As List(Of DiggStory) = GetHeadlines()
			Dim dataset As DS_Digg = New DS_Digg()
			For Each story As DiggStory In stories
				dataset.DiggStory.AddDiggStoryRow(story.Title, story.Date, story.Description, story.Diggs, story.Url, story.Topic, story.TopicContainer)
			Next story
			Return dataset
		End Function
		#End Region

		#Region "Private Functions"
		''' <summary>
		''' Parses the responseData and creates 'storyList' by populating DiggStory objects
		''' </summary>
		''' <param name="responseData">Query response data</param>
		''' <returns>List of Digg Stories</returns>
		Private Shared Function ParseResponseXml(ByVal responseData As String) As List(Of DiggStory)
			Dim storyList As List(Of DiggStory) = New List(Of DiggStory)()

			Dim xmlDocument As XmlDocument = New XmlDocument()
			xmlDocument.LoadXml(responseData)
			Dim storyNodeList As XmlNodeList = xmlDocument.SelectNodes("stories/story")
			Dim story As DiggStory = Nothing
			For Each storyNode As XmlNode In storyNodeList
				'Populate DiggStory object
				story = New DiggStory()
				story.Url = storyNode.Attributes("link").Value
				story.Diggs = Int32.Parse(storyNode.Attributes("diggs").Value, CultureInfo.InvariantCulture)
				' Set a base date to enable conversion of Unix timestamps returned in xml to 
				' DateTime object
				Dim baseDateTime As System.DateTime = New System.DateTime(1970, 1, 1, 0, 0, 0, 0)
				story.Date = baseDateTime.AddSeconds(Convert.ToDouble(storyNode.Attributes("submit_date").Value, CultureInfo.InvariantCulture))

				story.Title = storyNode.SelectSingleNode("title").InnerText
				story.Description = storyNode.SelectSingleNode("description").InnerText
				story.Topic = storyNode.SelectSingleNode("topic").Attributes("name").Value
				story.TopicContainer = storyNode.SelectSingleNode("container").Attributes("name").Value
				'Add populated object to the list
				storyList.Add(story)
			Next storyNode
			Return storyList
		End Function
		#End Region
	End Class

	''' <summary>
	''' Represents a story Digg.com 
	''' </summary>
	Public Class DiggStory
		#Region "Private Members"
		''' <summary>
		''' Title of the story
		''' </summary>
		Private title_Renamed As String
		''' <summary>
		''' Submit Date
		''' </summary>
		Private date_Renamed As DateTime
		''' <summary>
		''' Description of the story 
		''' </summary>
		Private description_Renamed As String
		''' <summary>
		''' No. of diggs
		''' </summary>
		Private diggs_Renamed As Integer
		''' <summary>
		''' Url with the details about the story
		''' </summary>
		Private url_Renamed As String
		''' <summary>
		''' Story Sub Topic
		''' </summary>
		Private topic_Renamed As String
		''' <summary>
		''' Story Main Topic
		''' </summary>
		Private topicContainer_Renamed As String
		#End Region

		#Region "Public Properties"
		''' <summary>
		''' Title of the story
		''' </summary>
		Public Property Title() As String
			Get
				Return Me.title_Renamed
			End Get
			Set(ByVal value As String)
				Me.title_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Date when the story was submitted
		''' </summary>
		Public Property [Date]() As DateTime
			Get
				Return Me.date_Renamed
			End Get
			Set(ByVal value As DateTime)
				Me.date_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Description of the story 
		''' </summary>
		Public Property Description() As String
			Get
				Return Me.description_Renamed
			End Get
			Set(ByVal value As String)
				Me.description_Renamed = value
			End Set
		End Property
		''' <summary>
		''' No. of diggs
		''' </summary>
		Public Property Diggs() As Integer
			Get
				Return Me.diggs_Renamed
			End Get
			Set(ByVal value As Integer)
				Me.diggs_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Url with the details about the story
		''' </summary>
		Public Property Url() As String
			Get
				Return Me.url_Renamed
			End Get
			Set(ByVal value As String)
				Me.url_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Story Sub Topic
		''' </summary>
		Public Property Topic() As String
			Get
				Return Me.topic_Renamed
			End Get
			Set(ByVal value As String)
				Me.topic_Renamed = value
			End Set
		End Property
		''' <summary>
		''' Story Main Topic
		''' </summary>
		Public Property TopicContainer() As String
			Get
				Return Me.topicContainer_Renamed
			End Get
			Set(ByVal value As String)
				Me.topicContainer_Renamed = value
			End Set
		End Property
		#End Region
	End Class
End Namespace